package org.linlinjava.litemall.admin.web;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.dto.GoodsAllinone;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallNews;
import org.linlinjava.litemall.db.service.LitemallNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Date;

@RestController
@RequestMapping("/admin/news")
@Validated
public class AdminNewsController {

    @Autowired
    LitemallNewsService litemallNewsService;

    @RequiresPermissions("admin:news:list")
    @RequiresPermissionsDesc(menu = {"新闻管理", "新闻管理"}, button = "列表")
    @GetMapping("/list")
    public Object list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @Sort @RequestParam(defaultValue = "add_time") String sort,
            @Order @RequestParam(defaultValue = "desc") String order) {
        return ResponseUtil.ok(litemallNewsService.selectallnews(page,limit,sort,order));
    }

    @RequiresPermissions("admin:news:update")
    @RequiresPermissionsDesc(menu = {"新闻管理", "修改新闻"}, button = "编辑")
    @PostMapping("/update")
    public Object updateRequestParam(@RequestParam(defaultValue = "0") Integer id,
                                     @RequestParam(defaultValue = "") String title,
                                     @RequestParam(defaultValue = "") String context,
                                     @RequestParam(defaultValue = "") String picUrl,
    @RequestParam(defaultValue = "") String newstype ) {

        LitemallNews litemallNews =new LitemallNews();
        litemallNews.setId(id);
        litemallNews.setTitle(title);
        litemallNews.setContext(context);
        litemallNews.setPicUrl(picUrl);
        litemallNews.setNewstype(newstype);
        return ResponseUtil.ok(litemallNewsService.updatenewsByid(litemallNews));
    }
    @RequiresPermissions("admin:news:save")
    @RequiresPermissionsDesc(menu = {"新闻管理", "保存新闻"}, button = "添加")
    @PostMapping("/save")
    public Object save(@RequestBody LitemallNews news){
        if (news != null) {
            news.setAddTime(new Date());
            news.setLooks(0);
            news.setDianzan(0);
            news.setCai(0);
            news.setZhuanfa(0);
            news.setShoucang(0);
            news.setPinglun(0);
            news.setIsdelete(0);
            news.setUpdateTime(new Date());
            litemallNewsService.save(news);

            return ResponseUtil.ok("添加成功");
        }
        return ResponseUtil.ok("添加失败");
    }

    @RequiresPermissions("admin:news:delete")
    @RequiresPermissionsDesc(menu = {"新闻管理", "删除新闻"}, button = "删除")
    @GetMapping("/delete")
    public Object delete(@RequestParam(defaultValue = "0") Integer id) {
        LitemallNews news=new LitemallNews();
        news.setId(id);
        news.setIsdelete(1);

        return  ResponseUtil.ok(litemallNewsService.updateshujv(news));
    }


    @GetMapping("/user/detail")
    public Object userdetail(@RequestParam(defaultValue = "0") Integer id) {
        LitemallNews news=new LitemallNews();
        news.setId(id);
        news.setLooks(1);
        litemallNewsService.updateshujv(news);
        return ResponseUtil.ok(litemallNewsService.selectByid(id)) ;
    }


}
